<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
      <el-breadcrumb-item>首页</el-breadcrumb-item>
      <el-breadcrumb-item>系统管理</el-breadcrumb-item>
      <el-breadcrumb-item>数据字典管理</el-breadcrumb-item>
      <el-breadcrumb-item>数据字典修改</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区 -->
    <el-card class="box-card">
      <!-- 搜索与添加区域 -->
      <el-row :gutter="20" class="el-row-table">
        <el-col :span="20">
          <div class="grid-content bg-purple">
            <el-button type="primary" @click="toDict">返回</el-button>
          </div>
        </el-col>

        <el-col :span="2">
          <div class="grid-content bg-purple">
            <el-button type="primary" @click="addDictInfoDialogVisible = true"
              >添加字典明细</el-button
            >
          </div>
        </el-col>
      </el-row>

      <!-- 字典填写区域 -->
      <el-form
        label-width="100px"
        :model="dictForm"
        :rules="dictFormRules"
        ref="dictFormRef"
      >
        <el-form-item label="字典名称" prop="note">
          <el-input type="text" v-model="dictForm.note" disabled></el-input>
        </el-form-item>
        <el-form-item label="字典关键字" prop="name">
          <el-input type="text" v-model="dictForm.name" disabled></el-input>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 卡片视图区 -->
    <el-card class="box-card">
      <!-- 列表区域 -->
      <el-table border stripe width="100%" :data="dataDictionaryInfoList">
        <el-table-column fixed prop="info" label="业务明细"> </el-table-column>

        <el-table-column fixed prop="code" label="业务代码"> </el-table-column>
        <el-table-column fixed prop="defaultFlag" label="默认值">
        </el-table-column>
        <!-- <el-table-column fixed prop="ranking" label="排名" width="100">
        </el-table-column> -->
        <el-table-column fixed label="操作">
          <template #default="scope">
            <!-- 第一个是编辑按钮 -->
            <el-tooltip
              effect="dark"
              content="更改默认值"
              placement="top"
              :enterable="false"
            >
              <el-button
                type="primary"
                icon="el-icon-chat-line-square"
                circle
                @click="editDictInfo(scope.$index, scope.row)"
              ></el-button>
            </el-tooltip>

            <!-- 第二个是上移按钮 -->
            <el-tooltip
              effect="dark"
              content="上移"
              placement="top"
              :enterable="false"
            >
              <el-button
                type="danger"
                icon="el-icon-arrow-up"
                circle
                @click="moveUp(scope.row)"
              ></el-button>
            </el-tooltip>

            <!-- 第三个是下移按钮 -->
            <el-tooltip
              effect="dark"
              content="下移"
              placement="top"
              :enterable="false"
            >
              <el-button
                type="success"
                icon="el-icon-arrow-down"
                circle
                @click="moveDown(scope.row)"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 更改默认值对话框 -->
    <el-dialog
      title="更改默认值"
      :visible.sync="editDictInfoDialogVisible"
      width="30%"
      center
    >
      <span>确定更换默认值: {{ this.editDictInfoObject.info }} 吗?</span>
      <!-- 底部区 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDictInfoDialogVisible = false">关 闭</el-button>
        <el-button type="primary" @click="updateDefault">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 上移对话框 -->
    <el-dialog
      title="上移"
      :visible.sync="moveDictInfoDialogVisible"
      width="30%"
      center
    >
      <span>上移: {{ this.editDictInfoObject.info }} 吗?</span>
      <!-- 底部区 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="moveDictInfoDialogVisible = false">关 闭</el-button>
        <el-button type="primary" @click="onmoveUp">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 下移对话框 -->
    <el-dialog
      title="下移"
      :visible.sync="moveDownDictInfoDialogVisible"
      width="30%"
      center
    >
      <span>下移: {{ this.editDictInfoObject.info }} 吗?</span>
      <!-- 底部区 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="moveDownDictInfoDialogVisible = false"
          >关 闭</el-button
        >
        <el-button type="primary" @click="onmoveDown">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 添加字典明细对话框 -->
    <el-dialog
      title="添加字典明细"
      :visible.sync="addDictInfoDialogVisible"
      width="50%"
      @close="addDictInfoDialogClosed"
      center
    >
      <!-- 内容主体区 -->
      <el-form
        :model="addDictInfoForm"
        :rules="addDictInfoRules"
        ref="addDictInfoRef"
        label-width="100px"
      >
        <el-form-item label="明细名称" prop="info">
          <el-input v-model="addDictInfoForm.info"></el-input>
        </el-form-item>

        <el-form-item label="默认值" prop="defaultFlag">
          <el-select v-model="addDictInfoForm.defaultFlag">
            <el-option label="是" value="true"></el-option>
            <el-option label="否" value="false"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="数值" prop="code">
          <el-input v-model="addDictInfoForm.code"></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部区 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDictInfoDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onSaveDictInfo">保存</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  pageDictInfo,
  updateDictInfoDefault,
  getDictInfoById,
  moveUpDictInfoDefault,
  moveDownDictInfoDefault,
  getDictById,
  saveDict,
} from "@/api/datadictionary.js";
export default {
  data() {
    const validateCode = (rule, value, callback) => {
      this.dictInfo.forEach((param) => {
        if (param.code === value) {
          callback(new Error("该值已经存在"));
          return;
        }
      });
      callback();
    };

    return {
      queryInfo: {
        current: 1,
        size: 5,
        name: "",
      },
      total: 0,
      dictForm: {
        name: "", //这个代表关键字
        note: "", //这个代表名称
      },
      //添加系统参数的系统规则
      dictFormRules: {
        name: [
          {
            required: true,
            message: "请输入关键字",
            trigger: "blur",
          },
        ],
        note: [
          {
            required: true,
            message: "请输入名称",
            trigger: "blur",
          },
        ],
      },
      dataDictionaryInfoList: [],
      //明细数组
      dictInfo: [],

      //编辑状态
      editDictInfoDialogVisible: false,
      //编辑对象
      editDictInfoObject: {},
      //上移弹窗
      moveDictInfoDialogVisible: false,
      //下移弹窗
      moveDownDictInfoDialogVisible: false,
      //控制添加数据字典明细对话框的显示与隐藏
      addDictInfoDialogVisible: false,
      //添加字典明细的表单
      addDictInfoForm: {
        info: "",
        defaultFlag: "",
        code: "",
        name: "", //字典关键词
        note: "", //字典名
      },
      //添加字典明细表单的验证规则
      addDictInfoRules: {
        info: [
          {
            required: true,
            message: "请输入关键字",
            trigger: "blur",
          },
        ],
        defaultFlag: [
          {
            required: true,
            message: "是否默认",
            trigger: "blur",
          },
        ],
        code: [
          {
            required: true,
            message: "请输入值",
            trigger: "blur",
          },
          {
            validator: validateCode,
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getDataDictionaryInfoList();
    this.getDictForm();
  },
  methods: {
    //添加数据字典明细界面关闭后刷新
    addDictInfoDialogClosed() {
      this.$refs.addDictInfoRef.resetFields();
    },
    // 保存明细
    onSaveDictInfo() {
      this.addDictInfoForm.name = this.dictForm.name; //获取字典关键字
      this.addDictInfoForm.note = this.dictForm.note; //获得字典名称

      saveDict(this.addDictInfoForm)
        .then((res) => {
          if (res.data.code !== 0) {
            return this.$message.error("请求出错");
          }

          this.addDictInfoDialogVisible = false;
          this.getDataDictionaryInfoList();
        })
        .catch(() => {
          this.$message.error("请求出错");
        });
    },

    //跳转到字典列表页
    toDict() {
      this.$router.push("/sys/dict");
    },
    async getDataDictionaryInfoList() {
      var id = JSON.parse(localStorage.getItem("checkId"));
      const { data: res } = await pageDictInfo(
        this.queryInfo.current,
        this.queryInfo.size,
        id
      );
      this.dataDictionaryInfoList = res.data.records;
      this.dataDictionaryInfoList.forEach((element) => {
        element.defaultFlag = element.defaultFlag ? "是" : "否";
      });

      this.total = res.data.total;
    },

    getDictForm() {
      var id = JSON.parse(localStorage.getItem("checkId"));
      getDictById(id)
        .then((res) => {
          if (res.data.code !== 0) {
            return this.$message.error("请求出错");
          }
          this.dictForm = res.data.data;
        })
        .catch(() => {
          this.$message.error("请求出错");
        });
    },
    //确认修改默认值
    updateDefault() {
      updateDictInfoDefault(this.editDictInfoObject.id)
        .then((res) => {
          if (res.data.code !== 0) {
            return this.$message.error("请求出错");
          }
          this.editDictInfoDialogVisible = false;
          this.getDataDictionaryInfoList();
        })
        .catch(() => {
          this.$message.error("请求出错");
        });
    },
    //获取行列信息
    editDictInfo(index, row) {
      getDictInfoById(row.id)
        .then((res) => {
          if (res.data.code !== 0) {
            return this.$message.error("请求出错");
          }
          this.editDictInfoDialogVisible = true;
          this.editDictInfoObject = res.data.data;
        })
        .catch(() => {
          this.$message.error("请求出错");
        });
    },
    //向上移动弹窗的显示与隐藏
    moveUp(row) {
      getDictInfoById(row.id)
        .then((res) => {
          if (res.data.code !== 0) {
            return this.$message.error("请求出错");
          }
          this.moveDictInfoDialogVisible = true;
          this.editDictInfoObject = res.data.data;
        })
        .catch(() => {
          this.$message.error("请求出错");
        });
    },
    //确认上移
    onmoveUp() {
      moveUpDictInfoDefault(this.editDictInfoObject.id)
        .then((res) => {
          if (res.data.code !== 0) {
            return this.$message.error("排名已经是第一啦");
          }
          this.moveDictInfoDialogVisible = false;
          this.getDataDictionaryInfoList();
        })
        .catch(() => {
          this.$message.error("请求出错");
        });
    },

    //向下移动弹窗的显示与隐藏
    moveDown(row) {
      getDictInfoById(row.id)
        .then((res) => {
          if (res.data.code !== 0) {
            return this.$message.error("请求出错");
          }
          this.moveDownDictInfoDialogVisible = true;
          this.editDictInfoObject = res.data.data;
        })
        .catch(() => {
          this.$message.error("请求出错");
        });
    },
    //确认下移
    onmoveDown() {
      moveDownDictInfoDefault(this.editDictInfoObject.id)
        .then((res) => {
          if (res.data.code !== 0) {
            return this.$message.error("排名已经是最后啦");
          }
          this.moveDownDictInfoDialogVisible = false;
          this.getDataDictionaryInfoList();
        })
        .catch(() => {
          this.$message.error("请求出错");
        });
    },
  },
};
</script>

<style lang="less" scoped>
.breadcrumb {
  margin-bottom: 15px;
  font-size: 15px;
}
.box-card {
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.15) !important;
  margin-top: 15px !important;
}
.el-row-table {
  margin-bottom: 15px !important;
}
.el-pagination {
  margin-top: 15px;
}
</style>